import redis
import importlib
import sys

import requests

from app.web.conf.config import base_url_cloud

importlib.reload(sys)
import json

class Get_redis():
    def __init__(self):
        pool = redis.ConnectionPool(host='10.21.32.3', port=6379, db=1, password='63KxsHOY4g939Apq')

        self.conn= redis.Redis(connection_pool=pool)

    def get_code(self,unique_code):

        # pool= redis.ConnectionPool(host='10.21.32.3', port=6379, db=1,password='63KxsHOY4g939Apq')
        # r = redis.Redis(connection_pool=pool)
        keys = self.conn.keys()#获取键
        print(keys)
        # uni="PICTURE_CODE_FOR_ACCOUNT_lOGIN_" + unique_code
        uni="PICTURE_CODE_FOR_ACCOUNT_lOGIN:"+unique_code
        # uni="ff1576121bf5cc1aa8ad97aabdc45be2286a88f5"
        print(uni)
        # print(type(uni))
        # k1=r.get(uni)
        # print(k1)
        # k2=r.get('ff1576121bf5cc1aa8ad97aabdc45be2286a88f5')
        # print(k2)
        k1=self.conn.get(uni).decode()
        # return(k1)

        return eval(k1)['check_code']


        #批量读取数据s

    def get_mobile_code(self,phone):
        mobile_code = self.conn.get('sms:code:' + phone)
        if mobile_code:
            mobile_code = mobile_code.decode('utf-8')
            mobile_code = json.loads(mobile_code, encoding='utf-8')['code']
        else:
            print('短信验证码为空')
        # self.conn.close()
        return mobile_code

    def get_sms(self,mobile):

        url=base_url_cloud +"/v3/client/reg-sms?sid=&token="
        data={"mobile":mobile}
        response = requests.post( url=url, json=data, verify=False)
        print(response.json())

if __name__=="__main__":
    G=Get_redis()
    # unique_code="picture_1577342839584157734283958415"
    # c=G.get_code(unique_code)
    # print(c)
    # print(type(c))
    d=G.get_mobile_code(phone="17666666666")
    print(d)
